package cn.itcast.nems.order.web.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.order.dto.NewOrderCustomerRelationshipDTO;
import cn.itcast.nems.order.dto.NewOrderDTO;
import cn.itcast.nems.order.dto.NewOrderDiscountDTO;
import cn.itcast.nems.order.dto.NewOrderLineDTO;
import cn.itcast.nems.order.web.vo.NewOrderVO;

import java.util.List;
import java.util.stream.Collectors;

public class OrderUtil {
    private OrderUtil() {}
    /**
     * 转化 DTO实体对象
     * @param order 请求对象
     * @return DTO实体
     */
    public static NewOrderDTO transferOrderDTO(NewOrderVO order) {
        NewOrderDTO orderDTO = CustomBeanUtil.copyProperties(order, new NewOrderDTO());
        NewOrderCustomerRelationshipDTO customerDTO = CustomBeanUtil.copyProperties(order.getCustomer(), new NewOrderCustomerRelationshipDTO());
        orderDTO.setOrderCustomerRelationship(customerDTO);
        // 如果是新订单 , 会出现只有订单头 , 没有订单行的情况
        if (CollectionUtil.isNotEmpty(order.getLines())) {
            List<NewOrderLineDTO> orderLines = order.getLines().stream()
                    .map(item -> CustomBeanUtil.copyProperties(item, new NewOrderLineDTO())).toList();
            orderDTO.setLines(orderLines);
        }
        // 优惠列表
        if (CollectionUtil.isNotEmpty(order.getDiscounts())) {
            for (int i = 0; i < order.getDiscounts().size(); i++) {
                order.getDiscounts().get(i).setSort((byte) (i + 1));
            }
            List<NewOrderDiscountDTO> discounts = order.getDiscounts().stream().map(item -> CustomBeanUtil.copyProperties(item, new NewOrderDiscountDTO())).collect(Collectors.toList());
            orderDTO.setDiscounts(discounts);
        }
        return orderDTO;
    }
}
